 package com.xpsoft.core.util;
 
 import java.io.PrintStream;
 import java.security.MessageDigest;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.commons.codec.binary.Base64;
 
 public class StringUtil
 {
   public static String convertQuot(String orgStr)
   {
     return orgStr.replace("'", "\\'").replace("\"", "\\\"");
   }
 
   public static synchronized String encryptSha256(String inputStr) {
     try {
       MessageDigest md = MessageDigest.getInstance("SHA-256");
 
       byte[] digest = md.digest(inputStr.getBytes("UTF-8"));
 
       return new String(Base64.encodeBase64(digest));
     }
     catch (Exception e)
     {
     }
     return null;
   }
 
   public static String htmlEntityToString(String dataStr)
   {
     int start = 0;
     int end = 0;
     StringBuffer buffer = new StringBuffer();
     while (start > -1) {
       int system = 10;
       if (start == 0) {
         int t = dataStr.indexOf("&#");
         if (start != t) start = t;
       }
       end = dataStr.indexOf(";", start + 2);
       String charStr = "";
       if (end != -1) {
         charStr = dataStr.substring(start + 2, end);
 
         char s = charStr.charAt(0);
         if ((s == 'x') || (s == 'X')) {
           system = 16;
           charStr = charStr.substring(1);
         }
       }
       try
       {
         char letter = (char)Integer.parseInt(charStr, system);
         buffer.append(new Character(letter).toString());
       } catch (NumberFormatException e) {
         e.printStackTrace();
       }
 
       start = dataStr.indexOf("&#", end);
       if (start - end > 1) {
         buffer.append(dataStr.substring(end + 1, start));
       }
 
       if (start == -1) {
         int length = dataStr.length();
         if (end + 1 != length) {
           buffer.append(dataStr.substring(end + 1, length));
         }
       }
     }
     return buffer.toString();
   }
 
   public static String stringToHtmlEntity(String str)
   {
     StringBuffer sb = new StringBuffer();
     for (int i = 0; i < str.length(); i++) {
       char c = str.charAt(i);
 
       switch (c) {
       case '\n':
         sb.append(c);
         break;
       case '<':
         sb.append("&lt;");
         break;
       case '>':
         sb.append("&gt;");
         break;
       case '&':
         sb.append("&amp;");
         break;
       case '\'':
         sb.append("&apos;");
         break;
       case '"':
         sb.append("&quot;");
         break;
       default:
         if ((c < ' ') || (c > '~')) {
           sb.append("&#x");
           sb.append(Integer.toString(c, 16));
           sb.append(';');
         } else {
           sb.append(c);
         }
       }
     }
     return sb.toString();
   }
 
   public static String stringToUnicode(String s)
   {
     String unicode = "";
     char[] charAry = new char[s.length()];
     for (int i = 0; i < charAry.length; i++) {
       charAry[i] = s.charAt(i);
       unicode = unicode + "\\u" + Integer.toString(charAry[i], 16);
     }
     return unicode;
   }
 
   public static String unicodeToString(String unicodeStr) {
     StringBuffer sb = new StringBuffer();
     String[] str = unicodeStr.toUpperCase().split("\\\\U");
     for (int i = 0; i < str.length; i++)
       if (!str[i].equals("")) {
         char c = (char)Integer.parseInt(str[i].trim(), 16);
         sb.append(c);
       }
     return sb.toString();
   }
 
   public static void main(String[] args)
   {
     String vm = "abc.em";
     System.out.println(stringToHtmlEntity(vm));
   }
 
   public static String html2Text(String inputString)
   {
     String htmlStr = inputString;
     String textStr = "";
     try
     {
       String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
 
       String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
 
       String regEx_html = "<[^>]+>";
 
       Pattern p_script = Pattern.compile(regEx_script, 
         2);
       Matcher m_script = p_script.matcher(htmlStr);
       htmlStr = m_script.replaceAll("");
 
       Pattern p_style = Pattern.compile(regEx_style, 
         2);
       Matcher m_style = p_style.matcher(htmlStr);
       htmlStr = m_style.replaceAll("");
 
       Pattern p_html = Pattern.compile(regEx_html, 
         2);
       Matcher m_html = p_html.matcher(htmlStr);
       htmlStr = m_html.replaceAll("");
 
       textStr = htmlStr;
     }
     catch (Exception e) {
       System.err.println("Html2Text: " + e.getMessage());
     }
 
     return textStr;
   }
 }

